package com.health.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.health.common.entity.OrderSetup;
import com.health.mapper.OrderSetupMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSetupService {

    @Autowired
    private OrderSetupMapper orderSetupMapper;

    public List<Map<String,Object>> getOrderSetupByMonth(String month) {
        String begin = month + "-1";
        String end = month + "-31";
        QueryWrapper<OrderSetup> w = new QueryWrapper<>();
        w.between("orderDate",begin,end);
        List<OrderSetup> list = orderSetupMapper.selectList(w);
        List<Map<String,Object>> orderData = new ArrayList<>();
        for (OrderSetup orderSetup : list) {
            Map<String,Object> order = new HashMap<>();
            order.put("orderDate",orderSetup.getOrderDate().getDate());
            order.put("number",orderSetup.getNumber());
            order.put("reservations",orderSetup.getReservations());
            orderData.add(order);
        }
        return orderData;
    }

    @Transactional
    public Integer editNumberByOrderDate(OrderSetup orderSetup) {
        QueryWrapper<OrderSetup> wrapper = new QueryWrapper<>();
        wrapper.eq("orderDate",orderSetup.getOrderDate());
        OrderSetup orderSetup4DB = orderSetupMapper.selectOne(wrapper);
        if(orderSetup4DB == null){
           return orderSetupMapper.insert(orderSetup);
        }else {
            orderSetup4DB.setNumber(orderSetup.getNumber());
           return orderSetupMapper.updateById(orderSetup4DB);
        }
    }
}
